package com.wanda.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wanda.model.JsonGeneralModel;
import com.wanda.model.Reception_RecordModel;
import com.wanda.service.Reception_RecordService;
import com.wanda.util.DateFormat;



@Controller
@RequestMapping("qt/Reception_Record")
public class Reception_RecordController {
	
	@Resource
	private Reception_RecordService reception_recordService;
	
  
	//添加客户 
	@RequestMapping(value="/addReception_Record.do")
    public @ResponseBody Map<String,Object> add(String user_id,String number,String user_name,String customer_id,String customer_name,String reception_mode,String reception_time,String reception_content,String times){
		System.err.println("sdgfsdgdsgfsdgsdgsdgsdfgshdsfgsfgdsg进入登录"); 
		Reception_RecordModel reception_record=new Reception_RecordModel(); 
		
		reception_record.setUser_id(user_id);
		reception_record.setUser_name(user_name);
		reception_record.setNumber(number);
		reception_record.setCustomer_id(customer_id);
		reception_record.setCustomer_name(customer_name);
		reception_record.setReception_mode(reception_mode);  
		reception_time=DateFormat.getNowDate();
		reception_record.setReception_time(reception_time);
		reception_record.setReception_content(reception_content);
		reception_record.setTimes(times);
		reception_record.setUpdate_user(" ");
		//reception_record.setUpdate_time(DateFormat.getNowDate());
		//reception_record.setDelete_user(" ");
		//customer.setDelete_time(DateFormat.getNowDate());
		reception_record.setDelete_state("0");
		reception_recordService.addReception_Record(reception_record);
    	Map<String,Object> map = new HashMap<String,Object>();  
    	System.out.println("添加成功"); 
    	map.put("msg","成功");
    	return map;
    }
	
	
	//修改客户
		@RequestMapping(value="/updateReception_Record.do")
	    public @ResponseBody Map<String,Object> update(Reception_RecordModel reception_record){
		    
			// =new Reception_RecordModel(); 
			
//			reception_record.setUser_id(user_id);
//			reception_record.setNumber(number);
//			reception_record.setCustomer_id(customer_id);
//			reception_record.setReception_mode(reception_mode);   
//			reception_record.setReception_time(reception_time);
//			reception_record.setReception_content(reception_content);
//			reception_record.setTimes(times);
			reception_record.setUpdate_user(" ");
			reception_record.setUpdate_time(DateFormat.getNowDate());
			reception_record.setDelete_state("0");
			//reception_recordService.setDelete_user(" ");
			reception_recordService.updateReception_Record(reception_record);
	    	Map<String,Object> map = new HashMap<String,Object>();  
	    	System.out.println("修改成功"); 
	    	
	    	return map;
	    }
		
		
		//删除客户
		@RequestMapping(value="/deleteReception_Record.do")
			 public @ResponseBody Map<String,Object> delete(String reception_record_id){
				      
			        Reception_RecordModel reception_record=new Reception_RecordModel(); 
			        reception_record.setReception_record_id(reception_record_id);
					String delete_state="1"; 
					reception_record.setDelete_state(delete_state);
					
					
					reception_recordService.deleteReception_Record(reception_record);
			    	Map<String,Object> map = new HashMap<String,Object>();  
			    	System.out.println("删除成功"); 
			    	;
			    	return map;
			    }
				
		//按照ID查询
			@RequestMapping(value="/selectById.do")
			  public @ResponseBody JsonGeneralModel select(String reception_record_id){				
				    	 System.err.println(reception_record_id);
				    	 
				     	JsonGeneralModel jsonModel = new JsonGeneralModel();
				    	List<Reception_RecordModel> reception_record =null;
						try {
							reception_record=reception_recordService.selectById(reception_record_id);
							//System.out.println("customer:"+customer.get(0).getCustomer_name());
							jsonModel.setRows(reception_record); 
						} 
						catch (Exception e) {
							e.printStackTrace();
						}
						
						return jsonModel;
				    } 
				 
		  //查询所有
				 @RequestMapping(value="/selectAll.do")
				    public @ResponseBody JsonGeneralModel selectAll(String delete_state){
					 System.out.println("查询所有");
						JsonGeneralModel<Reception_RecordModel> jsonModel = new JsonGeneralModel();
						List<Reception_RecordModel> reception_record =null;
						try {
						    delete_state="0"; 
							reception_record=reception_recordService.selectAll(delete_state);
							jsonModel.setRows(reception_record);
						}catch (Exception e) {
							e.printStackTrace();
						}
						return jsonModel;
				    }
    
    //拼接sql查询
	 @RequestMapping(value="/selectBySql.do")
	    public @ResponseBody JsonGeneralModel selectBySql(int page,int rows,String searchSql){
		 System.out.println("SQL查询");
		 System.err.println("SearchSql:"+searchSql);
		 JsonGeneralModel<Reception_RecordModel> jsonModel = new JsonGeneralModel();
			List<Reception_RecordModel> reception_record =null;
			int pageIndex=(page-1)*rows;
			
			pageIndex =0;
			int pageSize=0;
			
			try {
				String delete_state="0";
				reception_record=reception_recordService.selectBySql(pageIndex,rows,searchSql);
				pageSize=reception_recordService.selectAll(delete_state).size() % rows ==0?
						 reception_recordService.selectAll(delete_state).size() / rows:
							reception_recordService.selectAll(delete_state).size() / rows +1;
	
		    			 jsonModel.setPage(page);
						 jsonModel.setRecords(reception_recordService.selectAll(delete_state).size());
						 jsonModel.setTotal(pageSize);
						 Map<String,Object> data=new HashMap<String,Object>();
						 data.put("rows",reception_record);
					jsonModel.setRows(reception_record);
			}catch (Exception e) {
				e.printStackTrace();
			}
			return jsonModel;  
	    }	 

		
}
